package com.ruoyi.bigbase.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.bigbase.domain.Integral;
import com.ruoyi.bigbase.service.IIntegralService;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.bigbase.domain.Customer;
import com.ruoyi.bigbase.service.ICustomerService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 客户信息Controller
 * 
 * @author bigbase
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/info")
public class CustomerController extends BaseController
{
    @Autowired
    private ICustomerService customerService;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private IIntegralService integralService;


    /**
     * 查询客户信息列表
     */
    @RequiresPermissions("customer:info:list")
    @GetMapping("/list")
    public TableDataInfo list(Customer customer)
    {
        startPage();
        List<Customer> list = customerService.selectCustomerList(customer);
        return getDataTable(list);
    }

    @InnerAuth
    @GetMapping("/getInfo/{id}")
    public R<Customer> getCustomerInfo(@PathVariable("id") Integer id){
        return R.ok(customerService.selectCustomerById(id));
    }

    /**
     * 导出客户信息列表
     */
    @RequiresPermissions("customer:info:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Customer customer)
    {
        List<Customer> list = customerService.selectCustomerList(customer);
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        util.exportExcel(response, list, "客户信息数据");
    }

    /**
     * 获取客户信息详细信息
     */
    @RequiresPermissions("customer:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(customerService.selectCustomerById(id));
    }

    /**
     * 新增客户信息
     */
//    @RequiresPermissions("customer:info:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Customer customer)
    {
        if(customerService.isEmailExist(customer.getEmail())!=null)
            return AjaxResult.error("邮箱已存在");
        if(customerService.isPhoneExist(customer.getPhone())!=null)
            return AjaxResult.error("手机号已存在");
        if (customerService.isUsernameExist(customer.getUserName())!=null)
            return AjaxResult.error("用户名已存在");
        customer.setCreateTime(DateUtils.getNowDate());
        customerService.insertCustomer(customer);
        Integer uId = customerService.getIdByUsername(customer.getUserName());
        if(integralService.getIntegralByUserId(uId)==null){
            Integral integral = new Integral();
            integral.setUserId(uId);
            integral.setIntegral(0);
            integralService.insertIntegral(integral);
        }
        SysUser user = new SysUser();
        user.setUserName(customer.getUserName());
        user.setPassword(customer.getPassword());
        user.setRoleIds(new Long[]{102L});
        user.setCreateBy("用户注册");
        user.setRemark("customer"+uId);
        user.setNickName(customer.getNickName());
        user.setPhonenumber(customer.getPhone());
        user.setEmail(customer.getEmail());
        remoteUserService.addCustomer(user, SecurityConstants.INNER);
        return AjaxResult.success();
    }

    /**
     * 修改客户信息
     */
    @RequiresPermissions("customer:info:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Customer customer)
    {
        return toAjax(customerService.updateCustomer(customer));
    }

    /**
     * 删除客户信息
     */
    @RequiresPermissions("customer:info:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(customerService.deleteCustomerByIds(ids));
    }
}
